﻿#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.dll"
#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.Runtime.dll"
#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.DocumentManagement.dll"
#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.Taxonomy.dll"

#load "Client.fs"

open Client
open Microsoft.SharePoint.Client
open Microsoft.SharePoint.Client.Application
open Microsoft.SharePoint.Client.Taxonomy
open Microsoft.SharePoint.Client.WebParts
open Microsoft.SharePoint.Client.DocumentManagement
open Microsoft.SharePoint.Client.DocumentSet
open System.Security;
open System;
open System.Xml;
open System.Collections.Generic
    
// =======================================================
// Work with files
// =======================================================

let web = Client.Context.Web

// Retrieve file by URL
let file = web.GetFileByServerRelativeUrl("/Documents/logo.png")
Client.Load file
let fileItem = file.ListItemAllFields
Client.Load fileItem

// Retrieve file by item
let list = web.Lists.GetByTitle("Documents")
let doc = list.GetItemById(1)
Client.Load doc

doc.Update()
Client.ExecuteQuery()

let docFile = doc.File
Client.Load docFile
Client.Load docFile.LockedByUser
docFile.LockedByUser.LoginName

// Copy file

docFile.CopyTo("test", true);
Client.ExecuteQuery()

// Versioning
let msg = "MajorVersion: " + file.MajorVersion.ToString() + "| MinorVersion: " + file.MinorVersion.ToString() + "| UIVersion: " + file.UIVersion.ToString() + "| UIVersionLabel: " + file.UIVersionLabel
printfn "%s\n" msg